package com.algrithom.array;

import com.algrithom.quene.Solution1;

import java.util.Arrays;
import java.util.List;
import java.util.Stack;
import java.util.stream.Collectors;

/**
 * 121. 买卖股票的最佳时机
 */
public class Solution10 {
    public static void main(String[] args){
        int[] nums = new int[]{-2,1};
        Solution10 solution10 = new Solution10();
        System.out.println(solution10.maxProfit(nums));
    }
    public int maxProfit(int[] prices) {
        int maxprofit = 0;
        int len = prices.length;
        List<Integer> list = Arrays.stream(prices).boxed().collect(Collectors.toList());
        list.add(-1);
        Stack<Integer> stack = new Stack<>();
        for (int i = 0; i <= len; i++) {
            while (!stack.isEmpty() && stack.peek() > list.get(i)) {
                if (stack.size() > 1) {
                    maxprofit = Math.max(maxprofit,stack.peek() - stack.firstElement());
                }
                stack.pop();
            }
            stack.push(list.get(i));
        }
        return maxprofit;
    }
}
